<?php

class Service_Model_DbTable_Customer extends Custom_Db_Table_Abstract
{

    public $_name = 'vt_customers';

    public function sortedFields()
	{
		$fields = array(
					'id'=>'master.id',
					'fname'=>'master.first_name',
					'lname'=>'master.last_name',
					'address'=>'master.permanent_address',
					'bname'=>'bank.name'
		);

		return $fields;
	}

   /**
	 * fetch all customer with their bank details
	 * @param int $bank_id
     * @param string searchKey
	 * @param array $fields array of fields to show
	 * @return array
	 */
	public function  fetchAllWithBank( $bank_id = null, $searchKey = null, $fields = "*")
	{
		$objBank = new Service_Model_DbTable_Bank();
		try
		{
           $select = $this->select()
							->setIntegrityCheck(false)
							->from(array('master' => $this->_name), array($fields))
							->join(array('bank' => $objBank->_name), 'bank.id = master.bank_id', array('bank.name as bankName', 'bank.address as bankAddress'));

            if($bank_id)
            {
                $select->where("master.bank_id = ?", $bank_id);
            }
            else if($this->_roleId == User_Model_DbTable_Role::ADMIN_USER)
            {
                $select->where("1 = 1");
            }
            else
            {
                $select->where("master.bank_id = ?", $this->_bankId);
            }

            #search condition
            if($searchKey)
            {
                $cond = '';
                foreach($this->sortedFields() as $field) $cond .= $field." like '".$searchKey."%' OR ";
                $where = substr_replace($cond, '', -3);
                $select->where($where);
            }
            
            #order by
            $select->order($this->getOrderBy());
            //echo $select->__toString();die;
            return $this->_applyPaginator($select)->toArray();
		}
		catch(Zend_Exception $e)
		{
			$this->setError($e->getMessage());
			return false;
		}
	}

    /**
	 * fetch a customer with their bank details
	 * @param int $bank_id
     * @param string searchKey
	 * @param array $fields array of fields to show
	 * @return array
	 */
	public function  fetchOneWithBank( $id, $bank_id = null, $fields = "*")
	{
		$objBank = new Service_Model_DbTable_Bank();
        
		try
		{
			$select = $this->select()
							->setIntegrityCheck(false)
							->from(array('master' => $this->_name), array($fields))
							->join(array('bank' => $objBank->_name), 'bank.id = master.bank_id',
                                   array('bank.name as bankName', 'bank.address as bankAddress'));

            if($bank_id)
            {
                $select->where("master.id = ?", $id);
                $select->where("master.bank_id = ?", $bank_id);
            }
            else if($this->_roleId == User_Model_DbTable_Role::ADMIN_USER)
            {
                $select->where("master.id = ?", $id);
            }
            else
            {
                $select->where("master.id = ?", $id);
                $select->where("master.bank_id = ?", $this->_bankId);
            }

            $row = $this->fetchRow($select);
           
            if($row) return $row->toArray();
            else return false;
		}
		catch(Zend_Exception $e)
		{
			$this->setError($e->getMessage());
			return false;
		}
	}


}

